const app = getApp()
import {
  gettodaydata,
  indexCountDown,
  getImage,
  getindexcoupon,
  getGoodsSort,
  referral,
  getTopProducts,
  getSortList
} from '../../utils/requestApi.js'
import {
  bindReferralId
} from "../../utils/login.js"
import {
  throttle
} from "../../utils/myutil.js"
import {
  countdown
} from '../../utils/util.js'
let timer = null; // 定时器
let timer2 = null; // 定时器

Page({
  data: {
    swiperH: 150,    //swiper高度
    statusBarHeight: null, //页面状态栏高度
    navHeight: null, // 页面导航栏高度
    showtarbar: false, //nav弹窗
    showtarbarAni: false, // nav弹窗动画
    navitemactive: -1, //nav选中分类的状态
    sharestutes: false, //分享
    coupons: {},
    colse: true, //优惠券
    // 小图标
    imgurl: app.data.imgurl,
    //  顶部商品分类导航栏
    Categorydata: [],
    groupdata: [], //拼团
    countDownList: [], //限时抢购数据 
    bannerArr: [], // 轮播图
    bannerArr2: [], //占位广告
    newpeopledata: [],
    newpeoplepage: 1, // 新人优惠
    newpeopleTotalRecords: 0, //新人特惠的数据总数
    zhekoudata: [],
    zhekoupage: 1,
    zhekouTotal: 0, // 折扣专区,
    hasedtimedata: [],
    Freeshippingdata: [],
    Freeshippingtotal: 0,
    Nightpage: 1, // 9.9包邮
    status: 0, //选中状态
    active: -1, //顶部选中的状态
    // 分享
    cardCur: 0,
    sepciledata: [], //今日特卖
    windowWidth: 0,
    windowHeight: 0,
    activeItem: 0, //分类激活项
    goodsList: [],
    hotGoods: [], // 人气推荐
    page: 1, //
    // 我的数据end
    imgUrls: [],
  },

  onLoad: function (options) {
    var _this = this
    this.setData({
      statusBarHeight: app.data.statusBarHeight,
      navHeight: app.data.navHeight
    })

    wx.stopPullDownRefresh()
    // 开启转发功能
    wx.showShareMenu({
      withShareTicket: true
    })

    var timer = setInterval(res =>{
      if (app.data.cookie) {
        this.getnewpeopelspecile()
        this.initBanner()
        this.getCategory()
        clearInterval(timer)
      }
    }, 100)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 底部自定义导航栏
    app.globalData.template.tabbar("tabBar", 0, this, app.data.cartNum) //0表示第一个tabba/最后一个表示购物车的长度
    
  },

  onHide() {
    
  },

  // 下拉刷新
  onPullDownRefresh: function () {
    this.getList();
  },

  // 上拉加载更多
  onReachBottom: function () { },
  
  // 跳转搜索  
  Tosearch() {
    wx.navigateTo({
      url: '/pages/searchGoods/searchGoods',
    })
  },

  TobrandDiscount() {
    wx.navigateTo({
      url: '/pages/brandDiscount/brandDiscount',
    })
  },
  //   跳转商品详情
  Toprodetai(e) {
    //   console.log("shanglp",e)
    wx.navigateTo({
      url: `/pages/goodsDetail/goodsDetail?prDid=${e.currentTarget.dataset.productid}&pagetype=${e.currentTarget.dataset.pagetype}`,
    })
  },

  // 监听滚动事件(保留),回到顶部的按钮
  onPageScroll: function (data) {
    throttle(() => {
      if (data.scrollTop > 400) {
        if (this.data.showToTop) return
        this.setData({
          showToTop: true
        })
      } else {
        if (!this.data.showToTop) return
        this.setData({
          showToTop: false
        })
      }
    }, 300, 100)()
  },

  // 分享(保留)
  onShareAppMessage: function (res) {
    
  },

  /**  
   * 图片加载完成事件,动态设置swiper高度
   * */
  onLoadImg: function (e) {
    if (this.data.swiperH !== 150) {
      return
    }
    var winWid = wx.getSystemInfoSync().windowWidth
    this.data.swiperH = winWid * e.detail.height / e.detail.width
    this.setData({
      swiperH: this.data.swiperH
    })
  },
  
  // 获取根据图片分类id获取图片
  initBanner() {
    getImage({
      CatetoryId: 12,
      action: 'GetBanner'
    }).then((res) => {
      if (res.statusCode == 200) this.setData({bannerArr: res.data.Result.Data})
      else console.log(res)
    })
  },

  // 获取热销数据
  getnewpeopelspecile() {
    wx.showLoading({ title: '加载中...' })
    getTopProducts({
      Top: 3,
      ShowOrder: 'SaleCounts',
      IsDrainaging: 2
    }).then(res =>{
      wx.hideLoading();
      if (res.data.Status == 'Success') this.setData({ hotGoods: res.data.Data})
    })
  },

  // 获取商品的分类
  getCategory() {
    getGoodsSort().then((res) => {
      if (res.statusCode == 200) {
        this.setData({ Categorydata: res.data.Result.Data })
        this.getList(this.data.Categorydata[0].CategoryId) 
      }
    })
  },
  
  // 选择分类
  selectTabbar(e){
    this.setData({ goodsList: [], page: 1 })
    this.getList(this.data.Categorydata[e.detail.index].CategoryId)               
  },

  //获取分类的商品
  getList(id){
    let { page, goodsList } = this.data;
    wx.showLoading({ title: '加载中...' })
    getSortList({
      pageSize: 10,
      pageIndex: page,
      IsDrainaging: 2,
      CategoryId: id
    }).then(res =>{
      wx.hideLoading();
      if (res.data.Result.Data.length != 0){
        this.setData({ 
          goodsList: [...goodsList, ...res.data.Result.Data],
          page: page + 1
        })
      }
    })
  },

  // 分享
  share(){
    this.selectComponent('#share').shareInfo();
  }
})